package com.stylefeng.guns.modular.system.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.InitConsigneeID;
import com.stylefeng.guns.core.util.InitConsignerID;
import com.stylefeng.guns.core.util.InitGoodsID;
import com.stylefeng.guns.core.util.KeyUtil;
import com.stylefeng.guns.modular.system.dao.ReceiveBasicInformationMapper;
import com.stylefeng.guns.modular.system.model.Collectmoney;
import com.stylefeng.guns.modular.system.model.Company;
import com.stylefeng.guns.modular.system.model.Consignee;
import com.stylefeng.guns.modular.system.model.Consigner;
import com.stylefeng.guns.modular.system.model.Goods;
import com.stylefeng.guns.modular.system.model.InitId;
import com.stylefeng.guns.modular.system.model.ModelCheck;
import com.stylefeng.guns.modular.system.model.ModelReceive;
import com.stylefeng.guns.modular.system.model.ReceiveBasicInformation;
import com.stylefeng.guns.modular.system.model.ReceiveGoodsInformation;
import com.stylefeng.guns.modular.system.model.ReceiveGoodsStock;
import com.stylefeng.guns.modular.system.model.Warehouse;
import com.stylefeng.guns.modular.system.service.ICollectmoneyService;
import com.stylefeng.guns.modular.system.service.ICompanyService;
import com.stylefeng.guns.modular.system.service.IConsigneeService;
import com.stylefeng.guns.modular.system.service.IConsignerService;
import com.stylefeng.guns.modular.system.service.IGoodsService;
import com.stylefeng.guns.modular.system.service.IInitIdService;
import com.stylefeng.guns.modular.system.service.IReceiveBasicInformationService;
import com.stylefeng.guns.modular.system.service.IReceiveGoodsInformationService;
import com.stylefeng.guns.modular.system.service.IReceiveGoodsStockService;
import com.stylefeng.guns.modular.warehouse.service.IWarehouseService;

/**
 * 收获登记控制器
 *
 * @author fengshuonan
 * @Date 2018-04-05 11:41:51
 */
@Controller
@RequestMapping("/receiveBasicInformation")
public class ReceiveBasicInformationController extends BaseController {

    private String PREFIX = "/system/receiveBasicInformation/";

    @Autowired
    private IReceiveBasicInformationService receiveBasicInformationService;
    @Autowired
    private IWarehouseService warehouseService;
    @Autowired
    private IConsignerService consignerService;
    @Autowired
    private IConsigneeService consigneeService;
    @Autowired
    private IReceiveGoodsInformationService receiveGoodsInformationService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ReceiveBasicInformationMapper receiveBasicInformationMapper;
    @Autowired
    private IReceiveGoodsStockService stockService;
    @Autowired
    private ICollectmoneyService collectmoneyService;
    @Autowired
    private ICompanyService companyService;
    
    //合同号生成
    @Autowired
    private IInitIdService initIdService;
    
    public  String init3ID(int id){
		String number = id+"";
		int size = 3-number.length();
		StringBuffer str = new StringBuffer();
		for(int i=0;i<size;i++){
			str.append("0");
		}
		str.append(number);
		return  str.toString();
	}
    
    private String getContractNumber(){
    	
    	Date now = new Date();
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		String nowDate = format.format(now);
		InitId initId  = initIdService.selectList(null).get(0);
		String dbNumber = initId.getContractId();
		String dbID = dbNumber.substring(8);
		String dbDate = dbNumber.substring(0, 8);
		if(nowDate.equals(dbDate)){
			int ID = Integer.parseInt(dbID);
			ID++;
			return nowDate+init3ID(ID);
		}else{
			return nowDate+"001";
		}
		
    }

    /**
     * 跳转到收获登记首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "receiveBasicInformation.html";
    }
    
    /**
     * 收货登记查询跳转
     */
    @RequestMapping("boss")
    public String index01() {
        return "/query/receiveBasicInformation/" + "receiveBasicInformation.html";
    }
   
    
    /**
     * 审核跳转
     */
    @RequestMapping("check")
    public String index02() {
    	return "/check/receiveBasicInformation/" + "receiveBasicInformation.html";
    }
    

    /**
     * 跳转到添加收货登记
     */
    @RequestMapping("/receiveBasicInformation_add")
    public String receiveBasicInformationAdd(Model model) {
    	String contractNumber = getContractNumber();
    	ShiroUser shiroUser = ShiroKit.getUser();
    	String username=shiroUser.getName();
    	String receiveGoodsId = InitGoodsID.getInstance().initUnDisorderID();
    	List<Warehouse> warehouses = warehouseService.selectList(null);
    	model.addAttribute("warehouses", warehouses);
    	model.addAttribute("username", username);
    	model.addAttribute("receiveContractNumber", contractNumber);
    	model.addAttribute("receiveGoodsId", receiveGoodsId);
        return PREFIX + "receiveBasicInformation_add.html";
    }

    /**
     * 打开制单员重新编辑的详情页面
     */
    @RequestMapping("/receiveBasicInformation_updatetrade/{receiveBasicInformationId}")
    public String receiveBasicInformationUpdate(@PathVariable Integer receiveBasicInformationId, Model model) {
    	
        ReceiveBasicInformation receiveBasicInformation = receiveBasicInformationService.selectById(receiveBasicInformationId);
        EntityWrapper<ReceiveGoodsInformation> entityWrapper = new EntityWrapper<>();
        Wrapper<ReceiveGoodsInformation> wrapper = entityWrapper.eq("foreign_key_contract_number", receiveBasicInformation.getReceive_contract_number());
        List<ReceiveGoodsInformation> receiveGoodsInformations = receiveGoodsInformationService.selectList(wrapper);
        List<Warehouse> warehouses = warehouseService.selectList(null);
        ModelReceive modelReceive = new ModelReceive();
        modelReceive.setReceiveBasicInformation(receiveBasicInformation);
        modelReceive.setReceiveGoodsInformations(receiveGoodsInformations);
        
        model.addAttribute("warehouses",warehouses);
        model.addAttribute("item",modelReceive);
        LogObjectHolder.me().set(modelReceive);
        return PREFIX + "receiveBasicInformation_edit.html";
    }
    
    /**
     * 打开审核员审核的详情页面
     */
    @RequestMapping("/receiveBasicInformation_updatecheck/{receiveBasicInformationId}")
    public String receiveBasicInformationUpdateCheck(@PathVariable Integer receiveBasicInformationId, Model model) {
    	
    	//获取审核员信息
    	ShiroUser shiroUser = ShiroKit.getUser();
    	String username=shiroUser.getName();
    	
        ReceiveBasicInformation receiveBasicInformation = receiveBasicInformationService.selectById(receiveBasicInformationId);
        EntityWrapper<ReceiveGoodsInformation> entityWrapper = new EntityWrapper<>();
        Wrapper<ReceiveGoodsInformation> wrapper = entityWrapper.eq("foreign_key_contract_number", receiveBasicInformation.getReceive_contract_number());
        List<ReceiveGoodsInformation> receiveGoodsInformations = receiveGoodsInformationService.selectList(wrapper);
        ModelReceive modelReceive = new ModelReceive();
        modelReceive.setReceiveBasicInformation(receiveBasicInformation);
        modelReceive.setReceiveGoodsInformations(receiveGoodsInformations);
        
        model.addAttribute("username",username);
        model.addAttribute("item",modelReceive);
        LogObjectHolder.me().set(modelReceive);
        return "/check/receiveBasicInformation/" + "receiveBasicInformation_edit.html";
    }
    
    /**
     * 审核员判定是否审核通过
     */
    @RequestMapping(value = "/updatecheck")
    @ResponseBody
    public Object updateCheck(@RequestBody ModelCheck modelCheck) {
    	int id = modelCheck.getId();
    	String type = modelCheck.getType();
    	//判断该登记状态是否还是未审核状态
    	ReceiveBasicInformation receiveBasicInformation = receiveBasicInformationService.selectById(id);
    	if(!receiveBasicInformation.getReceiveState().equals(KeyUtil.CHECK_STATE_UNCHECKED)){
    		return "FAIL";
    	}
    	if(type.equals("yes")){
    		//如果通过审核，则将其置为通过审核，并且合同状态为未开始,审核员记录
    		receiveBasicInformation.setReceiveState(KeyUtil.CHECK_STATE_SUCCESS_CHECKED);
    		receiveBasicInformation.setReceiveIsfinish(KeyUtil.FINISH_STATE_UNSTART);
    		
    		//并将该登记的货物信息增加到库存表中
    		String contractNumber = receiveBasicInformation.getReceive_contract_number();
    		EntityWrapper<ReceiveGoodsInformation> entityWrapper = new EntityWrapper<>();
        	Wrapper<ReceiveGoodsInformation> wrapper = entityWrapper.eq("foreign_key_contract_number", contractNumber);
    		List<ReceiveGoodsInformation> receiveGoodsInformations = receiveGoodsInformationService.selectList(wrapper);
    		for(ReceiveGoodsInformation receiveGoodsInformation:receiveGoodsInformations){
    			ReceiveGoodsStock receiveGoodsStock = new ReceiveGoodsStock();
    			receiveGoodsStock.setForeignContractNumber(contractNumber);
    			receiveGoodsStock.setForeignKeyReceiveGoodsId(receiveGoodsInformation.getReceiveGoodsId());
    			receiveGoodsStock.setReceiveGoodsName(receiveGoodsInformation.getReceiveGoodsName());
    			receiveGoodsStock.setReceiveGoodsQuantity((new Double(receiveGoodsInformation.getReceiveGoodsQuantity())).intValue());
    			stockService.insert(receiveGoodsStock);
    		}
    		
    		//将总费用记到收款表中
    		Double sumFee = receiveBasicInformation.getReceive_gross_freight()+receiveBasicInformation.getReceive_gross_messagefee()+receiveBasicInformation.getReceive_gross_pickup_charge();
    		Collectmoney collectmoney = new Collectmoney();
    		collectmoney.setContractNumber(contractNumber);
    		collectmoney.setUncollectMoney(sumFee);
    		collectmoneyService.insert(collectmoney);
    		
    		
    		//获取审核员信息
        	ShiroUser shiroUser = ShiroKit.getUser();
        	String username=shiroUser.getName();
        	receiveBasicInformation.setReceiveCheck(username);
    		receiveBasicInformationService.updateAllColumnById(receiveBasicInformation);
    		return "SUCCESS";
    	}else if(type.equals("no")){
    		receiveBasicInformation.setReceiveState(KeyUtil.CHECK_STATE_FAIL_UNCHECKED);
    		receiveBasicInformationService.updateAllColumnById(receiveBasicInformation);
    		return "SUCCESS";
    	}
    	return "FAIL";
    }
    
    /**
     * 反审核操作
     */
    @RequestMapping(value = "/antiAudit")
    @ResponseBody
    public Object antiAudit(@RequestBody ModelCheck modelCheck) {
    	int id = modelCheck.getId();
    	//判断该登记状态是否还是未开始状态
    	ReceiveBasicInformation receiveBasicInformation = receiveBasicInformationService.selectById(id);
    	if(!receiveBasicInformation.getReceiveIsfinish().equals(KeyUtil.FINISH_STATE_UNSTART)){
    		return "FAIL";
    	}else{
    		receiveBasicInformation.setReceiveState(KeyUtil.CHECK_STATE_UNCHECKED);
    		receiveBasicInformation.setReceiveIsfinish(null);
    		receiveBasicInformationService.updateAllColumnById(receiveBasicInformation);
    		
    		//反审核操作时将库存表清除
    		String contractNumber = receiveBasicInformation.getReceive_contract_number();
    		EntityWrapper<ReceiveGoodsStock> entityWrapper = new EntityWrapper<>();
        	Wrapper<ReceiveGoodsStock> wrapper = entityWrapper.eq("foreign_contract_number", contractNumber);
        	stockService.delete(wrapper);
        	//反审核时将收款表清除
        	EntityWrapper<Collectmoney> entityWrapper1 = new EntityWrapper<>();
        	Wrapper<Collectmoney> wrapper1 = entityWrapper1.eq("contract_number", contractNumber);
        	collectmoneyService.delete(wrapper1);
    		
    		return "SUCCESS";
    	}
    }
    
    /**
     * 获取收获登记列表,此权限用于查询所有已通过审核的功能
     */
    @RequestMapping(value = "/listbossquery")
    @ResponseBody
    public Object list(@RequestParam(required = false) String receiveContractNumber, @RequestParam(required = false) String startDate,
    		@RequestParam(required = false) String arrivalDate, @RequestParam(required = false) String consigner,
    		@RequestParam(required = false) String consignee,@RequestParam(required = false) String type,
    		
    		@RequestParam(required = false) String consignerMobile,@RequestParam(required = false) String consigneeMobile,@RequestParam(required = false) String consignerPlace
    		,@RequestParam(required = false) String consigneePlace,@RequestParam(required = false) String tradePerson
    		) {
    	Page<ReceiveBasicInformation> page = new PageFactory<ReceiveBasicInformation>().defaultPage();
    	List<ReceiveBasicInformation> result = receiveBasicInformationService.getReceiveBasic(page, receiveContractNumber, startDate, arrivalDate, consigner, consignee, type, consignerMobile, consigneeMobile, consignerPlace, consigneePlace, tradePerson, page.getOrderByField(), page.isAsc());
    	page.setRecords(result);
    	return super.packForBT(page);
    }
    
    /**
     * 打开审核员反审核打印等操作的详情页面
     */
    @RequestMapping("/receiveBasicInformation_updatequery/{receiveBasicInformationId}")
    public String receiveBasicInformationUpdateQuery(@PathVariable Integer receiveBasicInformationId, Model model) {
    	
        ReceiveBasicInformation receiveBasicInformation = receiveBasicInformationService.selectById(receiveBasicInformationId);
        EntityWrapper<ReceiveGoodsInformation> entityWrapper = new EntityWrapper<>();
        Wrapper<ReceiveGoodsInformation> wrapper = entityWrapper.eq("foreign_key_contract_number", receiveBasicInformation.getReceive_contract_number());
        List<ReceiveGoodsInformation> receiveGoodsInformations = receiveGoodsInformationService.selectList(wrapper);
        ModelReceive modelReceive = new ModelReceive();
        modelReceive.setReceiveBasicInformation(receiveBasicInformation);
        modelReceive.setReceiveGoodsInformations(receiveGoodsInformations);
        model.addAttribute("item",modelReceive);
        LogObjectHolder.me().set(modelReceive);
        Company company = companyService.selectList(null).get(0);
        model.addAttribute("company",company);
        return "/query/receiveBasicInformation/" + "receiveBasicInformation_edit.html";
    }
    
    /**
     * 获取收获登记,此用于查询制单员制作的“未通过、未审核”的表数据
     */
    @RequestMapping(value = "/listtrade")
    @ResponseBody
    public Object listTrade(){
    	Page<ReceiveBasicInformation> page = new PageFactory<ReceiveBasicInformation>().defaultPage();
    	EntityWrapper<ReceiveBasicInformation> entityWrapper = new EntityWrapper<>();
    	ShiroUser shiroUser = ShiroKit.getUser();
    	String username=shiroUser.getName();
		entityWrapper.eq("receive_state", KeyUtil.CHECK_STATE_UNCHECKED).or().eq("receive_state", KeyUtil.CHECK_STATE_FAIL_UNCHECKED);
		//倒序排序
		List<String> str= new ArrayList<>();
		str.add("id");
		 Wrapper<ReceiveBasicInformation> wrapper =entityWrapper.andNew().eq("receive_trade_person", username).orderDesc(str);
		page.setRecords(receiveBasicInformationMapper.selectPage(page,wrapper));
		return super.packForBT(page); 
    }
    
    /**
     * 获取收获登记,此用于审核员审核的“未审核”的表数据
     */
    @RequestMapping(value = "/listcheck")
    @ResponseBody
    public Object listCheck(){
    	Page<ReceiveBasicInformation> page = new PageFactory<ReceiveBasicInformation>().defaultPage();
    	EntityWrapper<ReceiveBasicInformation> entityWrapper = new EntityWrapper<>();
    	//倒序排序
    	List<String> str= new ArrayList<>();
		str.add("id");
    	Wrapper<ReceiveBasicInformation> wrapper = entityWrapper.eq("receive_state", KeyUtil.CHECK_STATE_UNCHECKED).orderDesc(str);
		page.setRecords(receiveBasicInformationMapper.selectPage(page,wrapper));
		return super.packForBT(page); 
    }
    

    /**
     * 新增收获登记
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestBody ModelReceive modelReceive) {
    	//判断发货人是否是从数据库中获取的
    	if(modelReceive.getConsignerID().equals("yes")){
    		
    	}else{
    		//从收货登记表中读取发货人并存储
    		Consigner cr = new Consigner();
    		cr.setConsignerName(modelReceive.getReceiveBasicInformation().getReceiveConsigner());
    		cr.setConsignerPlace(modelReceive.getReceiveBasicInformation().getReceiveConsignerPlace());
    		cr.setConsignerMobile(modelReceive.getReceiveBasicInformation().getReceiveConsignerTell());
    		cr.setConsignerId(InitConsignerID.getInstance().initDisorderID());
    		consignerService.insert(cr);
    	}
    	
    	//判断收货人是否是从数据库中获取的
    	if(modelReceive.getConsigneeID().equals("yes")){
    		
    	}else{
    		//从收货登记表中读取收货人并存储
    		Consignee ce = new Consignee();
    		ce.setConsigneeName(modelReceive.getReceiveBasicInformation().getReceiveConsignee());
    		ce.setConsigneePlace(modelReceive.getReceiveBasicInformation().getReceiveConsigneePlace());
    		ce.setConsigneeMobile(modelReceive.getReceiveBasicInformation().getReceiveConsigneeTell());
    		ce.setConsigneeId(InitConsigneeID.getInstance().initDisorderID());
    		consigneeService.insert(ce);
    	}
    	
    	List<ReceiveGoodsInformation> receiveGoodsInformations = modelReceive.getReceiveGoodsInformations();
    	for(ReceiveGoodsInformation goodInfo:receiveGoodsInformations){
    		
    		
    		if(goodInfo.getGoodsID()==null||goodInfo.getGoodsID().equals("")){
    			Goods good = new Goods();
    			good.setGoodsId(goodInfo.getReceiveGoodsId());
    			good.setGoodsName(goodInfo.getReceiveGoodsName());
    			good.setGoodsModel(goodInfo.getReceiveGoodsModel());
    			good.setGoodsPackage(goodInfo.getReceiveGoodsPackage());
    			good.setGoodsWarehouse(goodInfo.getReceiveGoodsWarehouseName());
    			goodsService.insert(good);
    		}else{
    			//如果前台的标志不为空，那么该货物就是从数据库中读取的，就不进行存储操作了
    		}
    		goodInfo.setForeignKeyContractNumber(modelReceive.getReceiveBasicInformation().getReceive_contract_number());
    		receiveGoodsInformationService.insert(goodInfo);
    	}
    	modelReceive.getReceiveBasicInformation().setReceiveState(KeyUtil.CHECK_STATE_UNCHECKED);
    	receiveBasicInformationService.insert(modelReceive.getReceiveBasicInformation());
    	String contractNumber = modelReceive.getReceiveBasicInformation().getReceive_contract_number();
       
    	 InitId initIdDB  = initIdService.selectList(null).get(0);
  		long dbNumber = Long.parseLong(initIdDB.getContractId().trim());
  		long number = Long.parseLong(contractNumber.trim());
  		if(number-dbNumber>0){
  			//增加成功后，使用掉一个号码
  			InitId initId = new InitId();
  			initId.setId(1);
  			initId.setContractId(contractNumber);
  			initIdService.updateById(initId);
  		}
    	return "SUCCESS";
    }
    
    /**
     * 制单员重新编辑时，删除物品操作
     */
    @RequestMapping(value = "/deleteedit")
    @ResponseBody
    public Object deleteEdit(Integer id){
    	ReceiveGoodsInformation receiveGoodsInformation = receiveGoodsInformationService.selectById(id);
    	System.err.println(id);
    	System.err.println(receiveGoodsInformation.getForeignKeyContractNumber());
    	 EntityWrapper<ReceiveBasicInformation> entityWrapper = new EntityWrapper<>();
         Wrapper<ReceiveBasicInformation> wrapper = entityWrapper.eq("receive_contract_number", receiveGoodsInformation.getForeignKeyContractNumber());
         ReceiveBasicInformation receiveBasicInformation = receiveBasicInformationService.selectList(wrapper).get(0);
         if(receiveBasicInformation.getReceiveState().equals(KeyUtil.CHECK_STATE_SUCCESS_CHECKED)){
        	 return "FAIL";
         }
         receiveGoodsInformationService.deleteById(id);
    	return "SUCCESS";
    }
    
    /**
     * 删除收获登记
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer receiveBasicInformationId) {
    	ReceiveBasicInformation receiveBasicInformation = receiveBasicInformationService.selectById(receiveBasicInformationId);
    	//判断需要删除的收货登记是否已经通过审核了，如果通过审核，删除失败
    	if(!receiveBasicInformation.getReceiveState().equals(KeyUtil.CHECK_STATE_SUCCESS_CHECKED)){
    		//通过合同号删除收货登记货物表中的货物
    		String contractNumber = receiveBasicInformation.getReceive_contract_number();
    		EntityWrapper<ReceiveGoodsInformation> entityWrapper = new EntityWrapper<>();
    		Wrapper<ReceiveGoodsInformation> wrapper =entityWrapper.eq("foreign_key_contract_number", contractNumber);
    		receiveGoodsInformationService.delete(wrapper);
    		receiveBasicInformationService.deleteById(receiveBasicInformationId);
    		return SUCCESS_TIP;
    	}else{
    		return SUCCESS_TIP;
    	}
    }

    /**
     * 修改收获登记
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@RequestBody ModelReceive modelReceive) {
    	ReceiveBasicInformation re = receiveBasicInformationService.selectById(modelReceive.getReceiveBasicInformation().getId());
    	
    	
    	//先判断basic表中的合同是否已经被审核了
    	if(KeyUtil.CHECK_STATE_SUCCESS_CHECKED.equals(re.getReceiveState())){
    		return "FAIL";
    	}else{
    		//判断发货人是否是从数据库中获取的
        	if(modelReceive.getConsignerID().equals("yes")){
        		
        	}else{
        		//从收货登记表中读取发货人并存储
        		Consigner cr = new Consigner();
        		cr.setConsignerName(modelReceive.getReceiveBasicInformation().getReceiveConsigner());
        		cr.setConsignerPlace(modelReceive.getReceiveBasicInformation().getReceiveConsignerPlace());
        		cr.setConsignerMobile(modelReceive.getReceiveBasicInformation().getReceiveConsignerTell());
        		cr.setConsignerId(InitConsignerID.getInstance().initDisorderID());
        		consignerService.insert(cr);
        	}
        	
        	//判断收货人是否是从数据库中获取的
        	if(modelReceive.getConsigneeID().equals("yes")){
        		
        	}else{
        		//从收货登记表中读取收货人并存储
        		Consignee ce = new Consignee();
        		ce.setConsigneeName(modelReceive.getReceiveBasicInformation().getReceiveConsignee());
        		ce.setConsigneePlace(modelReceive.getReceiveBasicInformation().getReceiveConsigneePlace());
        		ce.setConsigneeMobile(modelReceive.getReceiveBasicInformation().getReceiveConsigneeTell());
        		ce.setConsigneeId(InitConsigneeID.getInstance().initDisorderID());
        		consigneeService.insert(ce);
        	}
        	
        	List<ReceiveGoodsInformation> receiveGoodsInformations = modelReceive.getReceiveGoodsInformations();
        	for(ReceiveGoodsInformation goodInfo:receiveGoodsInformations){
        		
        		if(goodInfo.getGoodsID()==null||goodInfo.getGoodsID().equals("")){
        			Goods good = new Goods();
        			good.setGoodsId(goodInfo.getReceiveGoodsId());
        			good.setGoodsName(goodInfo.getReceiveGoodsName());
        			good.setGoodsModel(goodInfo.getReceiveGoodsModel());
        			good.setGoodsPackage(goodInfo.getReceiveGoodsPackage());
        			good.setGoodsWarehouse(goodInfo.getReceiveGoodsWarehouseName());
        			goodsService.insert(good);
        		}else{
        			//如果前台的标志不为空，那么该货物就是从数据库中读取的，就不进行存储操作了
        		}
        		System.err.println(goodInfo.getId());
        		System.err.println("测试测试");
        		if(goodInfo.getId()==null||goodInfo.getId()==0){
        			goodInfo.setForeignKeyContractNumber(modelReceive.getReceiveBasicInformation().getReceive_contract_number());
        			receiveGoodsInformationService.insert(goodInfo);
        		}else{
        			receiveGoodsInformationService.updateById(goodInfo);
        		}
        	}
        	modelReceive.getReceiveBasicInformation().setReceiveState(KeyUtil.CHECK_STATE_UNCHECKED);
        	receiveBasicInformationService.updateAllColumnById(modelReceive.getReceiveBasicInformation());
            return "SUCCESS";
    	}
    }

    /**
     * 收获登记详情
     */
    @RequestMapping(value = "/detail/{receiveBasicInformationId}")
    @ResponseBody
    public Object detail(@PathVariable("receiveBasicInformationId") Integer receiveBasicInformationId) {
        return receiveBasicInformationService.selectById(receiveBasicInformationId);
    }
}
